
// init stuff

try {

    google.load("visualization", '1', {
        packages: ['geomap', 'columnchart']
    });
    
    var theMap = new Map();
    
    $(document).ready(function(){
    
        $("#demographics").hide();
        $("#map").hide();
        $("#chart_div").hide();
        $("#qfacts").hide();
        $("#buttons").hide();
        
        $("#hide").hide();
        
        // Set up the map div                    
        $("#dialog").dialog({
            autoOpen: false,
            modal: true,
            resizable: false
        });
        
        
        
        $('#opener').click(function(){
        
            $("#dialog").dialog('open');
        });
        
        
        theMap.init();
        $("#map").show();
		
        $('#buttons').show()
        
    });
    
    
} 
catch (e) {
    // alert("Error in setup code:" + e);
}



/******** Map Next Functions *********/


function updateMapSelection(){

    populationArray.length = 0;
    
    for (var i = 0; i < 50; i++) {
    
        if (theMap.data.getValue(i, 1) == 1) {
        
            var pop = newPopulation();
            pop.location = theMap.stateAbbr[i];
        }
    }
}


function mapNext(){

    try {
    
        $('#next').attr({
            onClick: "javascript:demographicsNext()",
            id: "next",
        });
        $('#qFactsToggle').hide();
        // Update what was selected and create population objects
        
        updateMapSelection();
        
        
        // store the results in the most current population class
        /****** ASSUMES THAT THERE IS A POPULATION CLASS CREATED *********/
        
        
        var newAccordian = $("#demographics").clone();
        manager.push(newAccordian);
        
        $("#accordion").accordion();
        
    } 
    catch (e) {
        // alert("Error in mapNext() " + e);
    }
}

function demographicsNext(){

    try {
    
        $('#next').attr({
            onClick: "javascript:location.reload(true)",
            id: "next",
        });
        $('#next').html("Start again");
        
        var index = populationArray.length;
        var recentPop = populationArray[index - 1];
        
        var coupleType;
        coupleType = $('#coupletype input:radio:checked').val();
        
        var ethnicity = $("#ethnicity input:radio:checked").val();
        var compare = $("#compare input:radio:checked").val();
        
        
        // update the info on all the population objects
        
        for (var i = 0; i < populationArray.length; i++) {
            var currentPop = populationArray[i];
            currentPop.coupleType = coupleType;
            currentPop.comparisonPoint = compare;
            currentPop.ethnicity = ethnicity;
            
            
            var sendString = "../server/query.aspx?location=" + currentPop.location + "&coupleStatus=" + currentPop.coupleType + "&ethnicity=" + currentPop.ethnicity + "&compare=" + currentPop.comparisonPoint;
            
            // alert("demographicsNext(): sendString is: " + sendString);
            
            
            
            manager.fetch(sendString);
            //// alert("demographicsNext(): "+sendString);
        }
        
        
        
        try {
        
        
        
            manager.push($("#chart_div"));
            
            
            
            var testResults = new Results("chart_div", "Comparisons");
            testResults.display();
            
            
        } 
        catch (e) {
            // alert("Error at demographicsNext(): " + e);
        }
        
        
        
    } 
    catch (e) {
        // alert("Error in DemographicsNext(): " + e);
    }
}



/********* Main Functions   *************/

function newPopulation(){
    try {
        // Create a population class and add it
        //// alert("setupIteration: Created a new population: "+manager.createPopulation());
        
        newPop = new Population();
        //newPop.location = "Los Angeles";
        
        populationArray[populationArray.length] = newPop;
        
        return newPop;
    } 
    catch (e) {
        // alert("Error in setupIteration: " + e);
    }
    
}
